package com.test.springcloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;

@SpringBootApplication
@RibbonClients
@RestController
public class RibbonDefaultConfigApplication {
	@Autowired
	private SpringClientFactory clientFactory;

	@RequestMapping("/")
	public void getServerList() throws Exception {
		ZoneAwareLoadBalancer<Server> lb = (ZoneAwareLoadBalancer<Server>) clientFactory.getLoadBalancer("server02");
		for (int i = 0; i < 10000; i++) {
			String server = lb.choose(null);
			System.out.println(server);			
		}
		System.out.println(lb.getLoadBalancerStats());
	}

	public static void main(String[] args) {
		SpringApplication app = new SpringApplicationBuilder(RibbonDefaultConfigApplication.class).build();
		app.run(args);
	}

}